﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net.NetworkInformation;
using Microsoft.Win32;

namespace Cobbler_MAC
{
    public partial class Form1 : Form
    {
        private dhcp operClassDHCP;
        public List<string> macList;
        public string macMask, adapterIP;
        private Thread dhcpThread = null;
        delegate void SimpleDelegate();
        public Form1()
        {
            InitializeComponent();
            textBox1.Text = (string)Registry.LocalMachine.GetValue("macMask", "00");
        }
        public void ExportFile(ListBox listBox)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                System.IO.StreamWriter sw = new System.IO.StreamWriter(saveFileDialog1.FileName);
                foreach (string item in listBox.Items)
                {
                    sw.WriteLine(item);
                }
                sw.Flush();
                sw.Close();
                MessageBox.Show("导出成功！");
                listBox1.Items.Clear();
            }
        }
        public void dhcp_Announced(cDHCPStruct d_DHCP, string macID)
        {
            string str = string.Empty;            
            if (macID.StartsWith(macMask) == true )
            {
                str = macID;
            }
            SimpleDelegate lst = delegate
            {
                if (!listBox1.Items.Contains(str))
                    listBox1.Items.Add(str);
            };
            Invoke(lst);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            switch (button1.Text)
            {
                case "Start":
                    //save the values to regisrtry
                    Registry.LocalMachine.SetValue("macMask", textBox1.Text);
                    adapterIP = "0.0.0.0";
                    textBox1.Enabled = false;
                    button2.Enabled = false;
                    button1.Text = "Stop";
                    listBox1.Items.Clear();
                    operClassDHCP = new dhcp(adapterIP);
                    operClassDHCP.Announced += new dhcp.AnnouncedEventHandler(dhcp_Announced);
                    macMask = textBox1.Text;
                    dhcpThread = new Thread(operClassDHCP.StartDHCPServer);
                    dhcpThread.Start();
                    break;
                default:
                    textBox1.Enabled = true;
                    button2.Enabled = true;
                    button1.Text = "Start";
                    operClassDHCP.Dispose();
                    break;
            }
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            Application.DoEvents();
            button1.PerformClick();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            operClassDHCP.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ExportFile(listBox1);
        }
    }
}
